package CCRMClient.GUI;

import javax.swing.JPanel;

import CCRMClient.ClientStart;
import Common.SERVER_COMMANDS;
import Common.Entities.Sale;

import java.awt.Color;

import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import javax.swing.border.LineBorder;

public class SaleDetailsScreen extends JPanel {

Sale sale;
private JLabel lblSale;
private JLabel lblSaleNumber;
private JLabel lblSaleName;
private JLabel lblSaleDescription;
private JLabel lblStartDate;
private JLabel lblFinishDate;
private JLabel lblTemplateNumber;
private JButton btnViewCustomers;
private JButton btnGoBack;
public SaleDetailsScreen(Sale sale) {
	setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
	this.sale =sale;
	
	
	initComponents();
}
private void initComponents() {
	setSize (518, 370);
	setBackground(Color.WHITE);
	setLayout(null);
	
	lblSale = new JLabel("Sale: " + sale.getSaleID());
	lblSale.setFont(new Font("David", Font.BOLD, 26));
	lblSale.setForeground(Color.RED);
	lblSale.setBounds(156, 11, 189, 40);
	add(lblSale);
	
	lblSaleNumber = new JLabel("Sale number: " + sale.getSaleID());
	lblSaleNumber.setFont(new Font("David", Font.BOLD, 24));
	lblSaleNumber.setBounds(10, 78, 279, 25);
	add(lblSaleNumber);
	
	lblSaleName = new JLabel("Sale name: " + sale.getSaleName());
	lblSaleName.setFont(new Font("David", Font.BOLD, 24));
	lblSaleName.setBounds(10, 114, 430, 25);
	add(lblSaleName);
	
	lblSaleDescription = new JLabel("Sale description: " + sale.getSaleDescription());
	lblSaleDescription.setFont(new Font("David", Font.BOLD, 24));
	lblSaleDescription.setBounds(10, 150, 430, 25);
	add(lblSaleDescription);
	
	String[] startDate = sale.getSaleStart().toString().split("-");
	lblStartDate = new JLabel("Start date: " + startDate[2] + "/" + startDate[1] + "/" + startDate[0]);
	lblStartDate.setFont(new Font("David", Font.BOLD, 24));
	lblStartDate.setBounds(10, 183, 481, 25);
	add(lblStartDate);
	
	String[] finishDate = sale.getSaleFinish().toString().split("-");
	lblFinishDate = new JLabel("Finish date: " + finishDate[2] + "/" + finishDate[1] + "/" + finishDate[0]);
	lblFinishDate.setFont(new Font("David", Font.BOLD, 24));
	lblFinishDate.setBounds(10, 219, 494, 25);
	add(lblFinishDate);
	
	int template = sale.getTemplateID();
	lblTemplateNumber = new JLabel("Template number: " + template );
	lblTemplateNumber.setFont(new Font("David", Font.BOLD, 24));
	lblTemplateNumber.setBounds(10, 255, 265, 25);
	add(lblTemplateNumber);
	
	btnViewCustomers = new JButton("View Customers");
	btnViewCustomers.addActionListener(new BtnViewCustomersActionListener());
	btnViewCustomers.setFont(new Font("Tahoma", Font.BOLD, 16));
	btnViewCustomers.setBounds(328, 291, 166, 23);
	add(btnViewCustomers);
	
	btnGoBack = new JButton("Go back");
	btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 16));
	btnGoBack.addActionListener(new BtnGoBackActionListener());
	btnGoBack.setBounds(10, 291, 133, 23);
	add(btnGoBack);
}
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			try {
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.VIEW_SALES_FOR_SALESMAN, ClientStart.user.getID());
			} catch (IOException e) {
				
				ClientStart.appWindow.notifyUser(e.getMessage(), "Sale Details", "ERRER_MESSAGE");
			}
		}
	}
	private class BtnViewCustomersActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String arr[]={Integer.toString(sale.getSaleID()),ClientStart.user.getID()};
			
			try {
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.SHOW_CUSTOMERS_FOR_SALESMAN, arr );
			} catch (IOException e1) {
		
				ClientStart.appWindow.notifyUser(e1.getMessage(), "Sale Details", "ERROR_MESSAGE");
			}
		}
	}
}
